package com.xr.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xr.common.annotation.SystemLog;
import com.xr.common.enums.LogType;
import com.xr.system.common.base.MyPage;
import com.xr.system.common.base.Result;
import com.xr.system.common.groups.Save;
import com.xr.system.common.groups.Update;
import com.xr.system.entity.roleModule.AssignmentPermissionDto;
import com.xr.system.entity.roleModule.Role;
import com.xr.system.service.RoleService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.List;

/**
 * 角色表(Role)表控制层
 */
@Validated
@RestController
@RequestMapping("/api/v1/role")
public class RoleController {

	/**
	 * 服务对象
	 */
	@Resource
	private RoleService roleService;

	/**
	 * 给角色分配系统权限
	 * @param assignmentPermissionDTO
	 * @return
	 */
	@SystemLog(module = "角色模块", type = LogType.INSERT, description = "给角色分配系统权限")
	@PostMapping("/assignmentPermissionForRole")
	public Result<Object> assignmentPermissionForRole(@RequestBody AssignmentPermissionDto assignmentPermissionDTO) {
		return Result.isSuccess(roleService.assignmentPermissionForRole(assignmentPermissionDTO));
	}

	/**
	 * 启用/禁用系统角色
	 * @param roleId
	 * @return
	 */
	@SystemLog(module = "角色模块", type = LogType.UPDATE, description = "启用/禁用系统角色")
	@PutMapping("/disOrEnaAccount")
	public Result<Object> disOrEnaAccount(@Validated @NotNull(message = "账号id不能为空") Long roleId) {
		Role role = new Role();
		LambdaUpdateWrapper<Role> updateWrapper = Wrappers.lambdaUpdate();
		updateWrapper.eq(Role::getInitial, false).setSql("enable = not enable").eq(Role::getId, roleId);
		return Result.isSuccess(roleService.update(role, updateWrapper));
	}

	/**
	 * 获取当前角色已选的权限
	 * @param id 角色id
	 * @return
	 */
	@GetMapping("/listCheckedPermissionForRole")
	public Result<List<Long>> listCheckedPermissionForRole(@Validated @NotNull(message = "角色id不能为空") Long id) {
		return Result.success(roleService.listCheckedPermissionForRole(id));
	}

	/**
	 * 获取用户关联的角色
	 * @param id 用户id
	 * @return
	 */
	@GetMapping("/roleByUserId")
	Result<List<Long>> getRoleByUserId(Long id) {
		return Result.success(roleService.getRoleByUserId(id));
	}

	/**
	 * 批量设置系统内置角色
	 * @param idList
	 * @return
	 */
	@SystemLog(module = "角色模块", type = LogType.UPDATE, description = "批量设置系统内置角色")
	@PutMapping("/setInitRole")
	public Result<Object> setInitRole(@RequestBody List<Long> idList) {
		return Result.isSuccess(roleService.setInitRole(idList));
	}

	/**
	 * 批量设置系统默认角色
	 * @param idList
	 * @return
	 */
	@SystemLog(module = "角色模块", type = LogType.UPDATE, description = "批量设置系统默认角色")
	@PutMapping("/setDefaultRole")
	public Result<Object> setDefaultRole(@RequestBody List<Long> idList) {
		return Result.isSuccess(roleService.setDefaultRole(idList));

	}

	/**
	 * 获取全部内置角色id
	 * @return
	 */
	@GetMapping("/listInitRole")
	public Result<List<Object>> listInitRole() {
		LambdaQueryWrapper<Role> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.eq(Role::getInitial, true).eq(Role::getEnable, true);
		queryWrapper.select(Role::getId);
		return Result.success(roleService.listObjs(queryWrapper));
	}

	/**
	 * 获取全部默认角色id
	 * @return
	 */
	@GetMapping("/listDefaultRole")
	public Result<List<Object>> listDefaultRole() {
		return Result.success(roleService.listDefaultRole());
	}

	/**
	 * 查询所有数据
	 * @param role 查询实体
	 * @return 所有数据
	 */
	@GetMapping("/queryAll")
	public Result<List<Role>> queryAll(Role role) {
		role.setEnable(true);
		return Result.success(roleService.list(new QueryWrapper<>(role)));
	}

	/**
	 * 分页查询所有数据
	 * @param page 分页对象
	 * @param role 查询实体
	 * @return 所有数据
	 */
	@GetMapping("/queryByPage")
	public Result<MyPage<Role>> queryByPage(MyPage<Role> page, Role role) {
		return Result.success(roleService.page(page, new QueryWrapper<>(role)));
	}

	/**
	 * 通过主键查询单条数据
	 * @param id 主键
	 * @return 单条数据
	 */
	@GetMapping("/queryOne/{id}")
	public Result<Role> selectOne(@PathVariable Serializable id) {
		return Result.success(roleService.getById(id));
	}

	/**
	 * 新增角色
	 * @param role 实体对象
	 * @return 新增结果
	 */
	@SystemLog(module = "角色模块", type = LogType.INSERT, description = "新增角色")
	@PostMapping("/insert")
	public Result<Object> insert(@Validated(Save.class) @RequestBody Role role) {
		return Result.isSuccess(roleService.insert(role));
	}

	/**
	 * 更新角色
	 * @param role 实体对象
	 * @return 修改结果
	 */
	@SystemLog(module = "角色模块", type = LogType.UPDATE, description = "更新角色")
	@PutMapping("/update")
	public Result<Object> update(@Validated(Update.class) @RequestBody Role role) {
		return Result.isSuccess(roleService.update(role));
	}

	/**
	 * 删除角色
	 * @param id 主键
	 * @return 删除结果
	 */
	@SystemLog(module = "角色模块", type = LogType.DELETE, description = "删除角色")
	@DeleteMapping("/delete/{id}")
	public Result<Object> delete(@PathVariable Long id) {
		Role role = new Role();
		LambdaUpdateWrapper<Role> updateWrapper = Wrappers.lambdaUpdate();
		updateWrapper.eq(Role::getId, id).eq(Role::getInitial, false);
		return Result.isSuccess(roleService.deleteWithFill(role, updateWrapper));
	}

	/**
	 * 批量删除角色
	 * @param idList 主键结合
	 * @return 删除结果
	 */
	@SystemLog(module = "角色模块", type = LogType.DELETE, description = "批量删除角色")
	@DeleteMapping("/batchDelete")
	public Result<Object> batchDelete(
			@Validated @Size(min = 1, message = "缺少删除所需的id") @RequestParam("idList") List<Long> idList) {
		Role role = new Role();
		role.setInitial(false);
		return Result.isSuccess(roleService.batchDelete(idList, role));
	}

}
